package com.qingke.easyjava.hibernate.dao;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.query.Query;

import com.qingke.easyjava.hibernate.bean.Accommodation;

public class AccommodationDao {
	private Session session;
	public AccommodationDao(Session session){
		this.session=session;
	}
	
	public Accommodation getAccommodationById(int id){
		return session.get(Accommodation.class, id);
	}
	
	public List<Accommodation> getAccommodationList(){
		Query<Accommodation> query=session.createQuery("from Accommodation", Accommodation.class);
		return query.getResultList();
	}
	
	public void insert(Accommodation ad){
		Transaction tx =session.beginTransaction();
		
		try{
			session.save(ad);
			tx.commit();
		}catch(HibernateException e){
			e.printStackTrace();
			tx.rollback();
		}
	}
	
	public void update(Accommodation ad){
		Transaction tx=session.beginTransaction();
		
		try{
			session.update(ad);
			tx.commit();
		}catch(HibernateException e){
			e.printStackTrace();
			tx.rollback();
		}
	}
	
	public void delete(Accommodation ad){
		Transaction tx=session.beginTransaction();
		
		try{
			session.delete(ad);
			tx.commit();
		}catch(HibernateException e){
			e.printStackTrace();
			tx.rollback();
		}
	}
}
